<html>
<head>
<title>Define Elements</title>
<link rel="stylesheet" href="css/general.css" type="text/css"/>
<script src="prototype.js" type="text/javascript"></script>
</head>
<body>
<h1>Define Elements <? echo $i;?></h1>

<?php
// Obtains variables from show project.php  
$id = $_GET['project_id'];
$i = $_GET['element_index'];
//if element index=0 then ignores the url variable statement; me made this to aviod an undefined variable error
if ($i!=0){
$statement = $_POST['statement'];
}

//Database connection
$dbc = mysqli_connect('localhost','root','','ism')
	or die("Unable to connect to MySQL");

if ($i!=0){
//Inserts element information in table element
$sql = "INSERT INTO element VALUES('','$statement', '$id','$i')"; 
$result = mysqli_query($dbc, $sql)
	or die('Query failed!');
}
echo('
<table>
	<tr>
		<td>');
		//Obtains the number of elements for the project that we are working with
		$sql2="SELECT number_elements FROM project WHERE id='$id'";
		$result = mysqli_query($dbc,$sql2)
			or die('Query failed!');
			while ($row = mysqli_fetch_array($result)){ 
			   $limit=$row['number_elements'];
			 }
			if (($i)==$limit){
				echo '<form method="post" action="show.php?project_id='.$id.'">';
				echo 'You have reached the limit of elements pre-stablished for this project.';
				echo '<input type=hidden name=limit value='.$limit.'>';
				echo '<p><input type="Submit" value="Start with matrix\'s"/></p>';
				}
			else{
				echo '<form method="post" action="add.php?project_id='.$id.'&element_index='.($i+1).'">';
				echo '<p><label>Name of Element '.($i+1).': </label><input type="text" size="40" name="statement"/></p>';
			echo '<p><input type="Submit" value="Add Element '.($i+1).'"/></p>';
			}
			echo '</form>
		</td>
		<td><table>
		';
		//Shows the elements that has been introduced up to this moment
		$sql3= "SELECT * FROM element WHERE project_id='$id'";
		$result = mysqli_query($dbc,$sql3)
			or die('Query failed!');
		$numero=1;
			while ($row = mysqli_fetch_array($result)){ 
			   echo $numero.": ".$row['statement'].'<br>';
			   $numero++;
			 }
		 echo'
		</table></td>
	</tr>
</table>';

?>
</body>
</html>
